package com.coldchain.cys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coldchain.common.exception.base.BusinessException;
import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.cys.constant.Constant;
import com.coldchain.cys.context.CysContext;
import com.coldchain.cys.entity.Cys;
import com.coldchain.cys.entity.CysCashLog;
import com.coldchain.cys.mapper.CysCashLogMapper;
import com.coldchain.cys.page.CysCashLogQueryVo;
import com.coldchain.cys.service.ICysAccountService;
import com.coldchain.cys.service.ICysCashLogService;
import com.coldchain.cys.service.ICysService;
import com.fangliai.core.context.BaseContextHandler;
import com.fangliai.core.entitiy.SystemModelEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianpeng
 * @since 2018-09-04
 */
@Service
public class CysCashLogServiceImpl extends ServiceImpl<CysCashLogMapper, CysCashLog> implements ICysCashLogService {

	@Autowired
	private ICysService cysService ;
	
	@Autowired
	private ICysAccountService accountService ;
	
	@Override
	public BigDecimal getCashingMoney() {
		Long cysid = CysContext.getCysID();
		return getCashingMoney(cysid) ;
	}
 
	@Override
	public BigDecimal getCashingMoney(Long cysid) {
		QueryWrapper<CysCashLog> ew = new QueryWrapper<CysCashLog>();
		ew.select("IFNULL(SUM(money),0) AS cashing") ;
		ew.eq("cid", cysid).eq("static", Constant.CYS_CASH_STATUS_ING) ;
		Map<String, Object> resultMap = super.getMap(ew);
		return (BigDecimal) resultMap.get("cashing") ;
	}

	@Override
	public void getList(CysCashLogQueryVo vo) {
		Long cysId = CysContext.getCysID();
		Cys cys = cysService.getById(cysId);
		QueryWrapper<CysCashLog> ew = new QueryWrapper<CysCashLog>();
		if(cys != null) {
			//如果当前是承运商来查看，只能查看他自己的
			ew.eq("cid", cys.getId()) ;
		}else if(vo.getCid() != null) {
			ew.eq("cid", vo.getCid()) ;
		}
		ew.orderByDesc("id") ;
		super.page(vo, ew) ;
	}

	@Override
	public void getCurrentList(CysCashLogQueryVo vo) {
		Long cysId = CysContext.getCysID();
		vo.setCid(cysId); 
		this.getList(vo); ;
	}

	@Transactional
	@Override
	public BaseResponse doCash(BigDecimal cashMoney) {
		Long cysId = CysContext.getCysID();
		Cys cys = cysService.getById(cysId);
		if(cys == null) {
			return getErrorMessage("请先登录") ;
		}
		if(cashMoney == null) {
			return getErrorMessage("必须填写提现金额") ;
		}
		if(cashMoney.compareTo(new BigDecimal(100)) < 0) {
			return getErrorMessage("提现金额不能低于100元") ;
		}
		//String result = accountService.doHandleDetail(cysId, HandleTypeEnum.cash.getId(), Constant.moneyOut, cashMoney, "提现扣除");
//		if(StrUtil.isNotBlank(result)) {
//			return getErrorMessage(result) ;
//		}
		CysCashLog cashLog = new CysCashLog();
		cashLog.setCid(cysId);
		cashLog.setStatus(Constant.CYS_CASH_STATUS_ING);
		cashLog.setMoney(cashMoney);
		boolean flag = super.save(cashLog);
		if(!flag) {
			throw new BusinessException("提现异常") ;
		}
		return new ObjectRestResponse<>().succeed("提现成功") ;
	}

	
	private BaseResponse getErrorMessage(String message) {
		return new ObjectRestResponse<>().error(message) ;
	}

	@Transactional
	@Override
	public BaseResponse doHandleCash(CysCashLog cash) {
		String sysModel = BaseContextHandler.getSysModl();
		if(!sysModel.equals(SystemModelEnum.ADMIN.name())) {
			return getErrorMessage("无权访问") ;
		}
		if(cash.getId() == null) {
			return getErrorMessage("参数错误") ;
		}
		if(cash.getStatus() == null) {
			return getErrorMessage("参数错误") ;
		}
		if(cash.getStatus() != Constant.CYS_CASH_STATUS_SUCCESS && cash.getStatus() != Constant.CYS_CASH_STATUS_REJECT ) {
			//提现不是成功 也不是驳回那么肯定是参数错误啦
			return getErrorMessage("参数错误") ;
		}
		CysCashLog cashLog = super.getById(cash.getId());
		if(cashLog == null) {
			return getErrorMessage("参数错误") ;
		}
		if(cashLog.getStatus() != Constant.CYS_CASH_STATUS_ING) {
			return getErrorMessage("参数错误") ;
		}
		cashLog.setStatus(cash.getStatus());
		if(cashLog.getStatus() == Constant.CYS_CASH_STATUS_REJECT) {
			//提现驳回
//			String result = accountService.doHandleDetail(cashLog.getCid(), HandleTypeEnum.cash_reject.getId()
//					, Constant.moneyIn, cashLog.getMoney(), "提现驳回");
//			if(StrUtil.isNotBlank(result)) {
//				return getErrorMessage("处理失败") ;
//			}
		}
		boolean result = super.updateById(cashLog);
		if(!result) {
			throw new BusinessException("处理失败") ;
		}
		
		return new ObjectRestResponse<>().succeed("处理成功") ;
	}
}
